﻿#include "SelectSetSurfWidget.h"
#include "ui_SelectSetSurfWidget.h"
#include "OperatorsInterface/ParaWidgetInterfaceOperator.h"
#include "OperatorsInterface/GraphEventOperator.h"
#include "GUIFrame/MainWindow.h"
#include "GUIFrame/PropertyWidget.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITK_Kernel/FITKCore/FITKOperatorRepo.h"
#include "FITK_Kernel/FITKCore/FITKDataRepo.h"
#include "FITK_Interface/FITKInterfaceModel/FITKAbstractAssInstance.h"
#include "FITK_Interface/FITKInterfaceMesh/FITKUnstructuredMeshVTK.h"
#include <QTableWidgetItem>
#include "FITK_Interface/FITKInterfacePhysics/FITKAbstractSection.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaAbstractSection.h"

static bool IsHighLight = false;

namespace GUI
{
    SelectSetSurfaceWidgetBase::SelectSetSurfaceWidgetBase(EventOper::ParaWidgetInterfaceOperator* oper, QWidget* parent,Interface::FITKAbaAbastractSection* section)
        : GUICalculateWidgetBase(oper, parent),_section(section)
    {
        _ui = new Ui::SetSurfaceSelectWidget;
        _ui->setupUi( this );

        _ui->lb_info->setVisible(false);
        _ui->checkBoxHL->setChecked(IsHighLight);
        //设置控件基本属性
        _ui->tableWidget->setSelectionMode( QAbstractItemView::SingleSelection );
        _ui->tableWidget->setSelectionBehavior( QAbstractItemView::SelectRows );
        _ui->tableWidget->setEditTriggers( QAbstractItemView::NoEditTriggers );
        _ui->tableWidget->horizontalHeader( )->sectionResizeMode( QHeaderView::Stretch );

        QString title=tr("Select element set");
        if ( !title.isEmpty( ) )
            this->setWindowTitle( title );
        this->initializeTableItem();
    }

    SelectSetSurfaceWidgetBase::~SelectSetSurfaceWidgetBase( )
    {
        if ( _ui != nullptr ) delete _ui;
    }

    void SelectSetSurfaceWidgetBase::updateWidget( )
    {
        _ui->tableWidget->setRowCount( 0 );
        _ui->continueButton->setEnabled( false );
    }

    void SelectSetSurfaceWidgetBase::on_continueButton_clicked( )
    {
        int               row  = _ui->tableWidget->currentRow( );
        QTableWidgetItem* item = _ui->tableWidget->item( row, 0 );

        SetSurfInfo info;
        this->getSetSurfInfo( item, &info );
        //一个组件只有一种材料
        QString materialName =_section->getMaterialName(0);
        QString meshName = item->text();
        QString ComponentName ;
        _section->SetComponentID(info._componentID);
        _section->SetComponentName(meshName);

        //判断meshName的长度是否大于10
        if(meshName.length()>16)
        {
            meshName=meshName.left(16);
        }
        ComponentName = meshName + "-[" + materialName + "]";
        _section->setDataObjectName(ComponentName);

        if (_oper != nullptr) {
            //执行操作器
            _oper->execProfession();
        }

        GUI::MainWindow* mainWindow = dynamic_cast<GUI::MainWindow*>(FITKAPP->getGlobalData()->getMainWindow());
        if (mainWindow == nullptr)return;
        GUI::PropertyWidget* propertyWidget = mainWindow->getPropertyWidget();
        if (propertyWidget == nullptr)return;

        propertyWidget->init();
    }

    void SelectSetSurfaceWidgetBase::on_cancelButton_clicked( )
    {
        this->reject( );
    }

    void SelectSetSurfaceWidgetBase::on_tableWidget_itemClicked( QTableWidgetItem* item )
    {
        if ( item == nullptr ) return;
        _ui->continueButton->setEnabled( true );

        // 不需要高亮，直接退出
        if (!isHighLightItem()) return;
        item = this->getSelectRowItem();

        SetSurfInfo info;
        this->getSetSurfInfo( item, &info );

        EventOper::GraphEventOperator* graphOper = FITKOPERREPO->getOperatorT<EventOper::GraphEventOperator>("GraphPreprocess");
        if (graphOper == nullptr)return;
        graphOper->clearHighlight();
        graphOper->highlight(info._componentID);
    }

    void SelectSetSurfaceWidgetBase::on_checkBoxHL_stateChanged(int)
    {
        IsHighLight = _ui->checkBoxHL->isChecked();
        EventOper::GraphEventOperator* graphOper = FITKOPERREPO->getOperatorT<EventOper::GraphEventOperator>("GraphPreprocess");
        if (graphOper == nullptr)return;
        graphOper->clearHighlight();
    } 

    void SelectSetSurfaceWidgetBase::addTableItem(QString name, SetSurfInfo* data, QString type)
    {
        if ( data == nullptr ) return;
        const int rowPos = _ui->tableWidget->rowCount( );
        _ui->tableWidget->insertRow( rowPos );

        QTableWidgetItem* item = new QTableWidgetItem( name );
        item->setData( Qt::UserRole, QVariant::fromValue( data->_modelID) );
        item->setData( Qt::UserRole + 1, QVariant::fromValue( data->_componentID) );
//         item->setData( Qt::UserRole + 2, QVariant::fromValue( data->_setOrSurf ) );
//         item->setData( Qt::UserRole + 3, ( int )data->_module );

        _ui->tableWidget->setItem( rowPos, 0, item );
        _ui->tableWidget->setItem( rowPos, 1, new QTableWidgetItem( type ) );
    }

    void SelectSetSurfaceWidgetBase::initializeTableItem()
    {

        Interface::FITKUnstructuredMeshVTK* mesh = FITKAPP->getGlobalData()->getMeshDataFASTSIM<Interface::FITKUnstructuredMeshVTK>();
        auto manager = mesh->getBoundaryMeshManager();
        int num = manager->getDataCount();
        for(int i=0;i<num;++i)
        {
            QString name = manager->getDataByIndex(i)->getDataObjectName();
            int id = manager->getIDByIndex(i);
            SetSurfInfo* data = new SetSurfInfo();
            data->_modelID = 1;
            data->_componentID = id;
            QString type = "Element";
            addTableItem(name,data,type);
        }
    }

    void SelectSetSurfaceWidgetBase::getSetSurfInfo( QTableWidgetItem* item, SetSurfInfo* info )
    {
        if ( item == nullptr || info == nullptr ) return;

        info->_modelID  = item->data( Qt::UserRole ).toInt();
        info->_componentID = item->data( Qt::UserRole + 1 ).toInt();
    }

    bool SelectSetSurfaceWidgetBase::isHighLightItem( )
    {
        return _ui->checkBoxHL->isChecked( );
    }

    QTableWidgetItem* SelectSetSurfaceWidgetBase::getSelectRowItem( )
    {
        int row  = _ui->tableWidget->currentRow( );
        return _ui->tableWidget->item( row, 0 );
    }

    void SelectSetSurfaceWidgetBase::reject( )
    {
        GUI::MainWindow* mainWindow = dynamic_cast<GUI::MainWindow*>(FITKAPP->getGlobalData()->getMainWindow());
        if (mainWindow == nullptr)return;
        GUI::PropertyWidget* propertyWidget = mainWindow->getPropertyWidget();
        if (propertyWidget == nullptr)return;

        propertyWidget->init();
    }

}  // namespace GUI
